﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LSBLL;
using LSEntity;
using LSUtility;
using OralExamEntity;

namespace OralExamBLL
{
    public class OECache<TModel> : MemoryBLL<TModel>
    {
        public OECache(MemoryEntity memoryEntity)
            : base(memoryEntity)
        {

        }

        #region TestStepSetCache

        public static object TestStepObject = new object();
        public static TestStepEntity TestStepGetCacheMine(int accountId,int testId)
        {
            var cacheData = OECache<TestStepEntity>.MemoryListGet(OEWebConfig.TestStepCacheKey);
            return cacheData != null
                       ? cacheData.FirstOrDefault(m => m.TestId == testId && m.AccountId == accountId)
                       : null;
        }

        public static void TestStepSetCache(MemberEntity member,
                                            int testId,
                                            bool? isLogin = null,
                                            bool? isMicrophone = null,
                                            bool? isPass = null,
                                            bool? isComplete = null,
                                            bool? isUp = null)
        {
            TestStepSetCache(member.AccountId,
                             testId,
                             classId: member.MyClass.ClassId,
                             className: member.MyClass.ClassName,
                             number: member.Account,
                             name: member.RealName,
                             isLogin: isLogin,
                             isMicrophone: isMicrophone,
                             isPass: isPass,
                             isComplete: isComplete,
                             isUp: isUp);
        }

        public static void TestStepSetCache(int accountId,
                                            int testId,
                                            int? classId = null,
                                            string className = null,
                                            string number = null,
                                            string name = null,
                                            bool? isLogin = null,
                                            bool? isMicrophone = null,
                                            bool? isPass = null,
                                            bool? isComplete = null,
                                            bool? isUp = null)
        {
            //var myList = listModel as List<TestStepEntity>;
            //if (myList == null) return;
            lock (TestStepObject)
            {
                var myTest = TestStepGetCacheMine(accountId, testId) ?? new TestStepEntity
                                                                            {
                                                                                AccountId = accountId,
                                                                                TestId = testId,
                                                                                Computer = BaseTool.ComputerName,
                                                                            };
                if (!String.IsNullOrEmpty(className)) myTest.ClassName = className;
                if (!String.IsNullOrEmpty(number)) myTest.Number = number;
                if (!String.IsNullOrEmpty(name)) myTest.Name = name;
                if (isLogin != null) myTest.IsLogin = isLogin.Value;
                if (isMicrophone != null) myTest.IsMicrophone = isMicrophone.Value;
                if (isPass != null) myTest.IsPass = isPass.Value;
                if (isComplete != null) myTest.IsComplete = isComplete.Value;
                if (isUp != null) myTest.IsUp = isUp.Value;
                if (classId != null) myTest.ClassId = classId.Value;

                var cacheBll = new OECache<TestStepEntity>(new MemoryEntity
                                                               {
                                                                   GetData = () => new List<TestStepEntity> { myTest },
                                                                   MemoryKey = OEWebConfig.TestStepCacheKey,
                                                                   TimeSpan = new TimeSpan(0, 60, 0),
                                                               });
                var list = cacheBll.MemoryModelList().Where(m => m != null).ToList() ?? new List<TestStepEntity>();
                list = list.ListToAddOrModify(myTest,
                                              n =>
                                              n.AccountId == myTest.AccountId &&
                                              n.TestId == myTest.TestId);
                cacheBll.MemorySet(list);
            }
        }

        #endregion

    }
}
